﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Flexibles.CApCableParametersNewUserControl
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shapes;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;
using UiInternalControls.WPFControls;


namespace DnProcessSimulateCommands.Flexibles;

public class CApCableParametersNewUserControl : UserControl, IComponentConnector
{
  private const double ForceParameterEpsilon = 0.01;
  private const double ParameterEpsilon = 0.0001;
  private bool m_isControlLoading;
  private readonly ICollection<IDisposable> m_disposableObjects;
  internal StackPanel parameterStackPanel;
  internal Grid parameterGrid;
  internal RowDefinition densityRow;
  internal RowDefinition fulcrumRow;
  internal RowDefinition maxTensionRow;
  internal RowDefinition maxTwistRow;
  internal RowDefinition minCurvatureRadiusRow;
  internal RowDefinition passNearRow;
  internal RowDefinition oscillationSliderRow;
  internal TextBlock textblockDensity;
  internal TxNumericEditBoxControl TxNumericEditBoxDensity;
  internal TextBlock textblockYoungsModulus;
  internal TxNumericEditBoxControl TxNumericEditBoxYoungsModulus;
  internal TextBlock labelCableFulcrumLength;
  internal TxNumericEditBoxControl TxNumericEditBoxFulcrumLength;
  internal TxInfoButton TxInfoButtonStartEndFulcrum;
  internal Grid tensionGrid;
  internal ColumnDefinition tensionColumn;
  internal Rectangle rectTensionViolationColor;
  internal TextBlock textblockMaxTension;
  internal TxNumericEditBoxControl TxNumericEditBoxMaxTension;
  internal Grid twistGrid;
  internal ColumnDefinition twistColumn;
  internal Rectangle rectTwistViolationColor;
  internal CheckBox textblockMaxTwist;
  internal TxNumericEditBoxControl TxNumericEditBoxMaxTwist;
  internal Grid contactForceGrid;
  internal ColumnDefinition contactForceColumn;
  internal Rectangle rectContactForceExcessViolationColor;
  internal CheckBox textblockMaxContactForce;
  internal TxNumericEditBoxControl TxNumericEditBoxMaxContactForce;
  internal Grid curvatureRadiusGrid;
  internal ColumnDefinition curvatureColumn;
  internal Rectangle rectBendingViolationColor;
  internal CheckBox textblockMinCurvatuceRadius;
  internal TxNumericEditBoxControl TxNumericEditBoxMinCurvatuceRadius;
  internal TextBlock labelCablePassNearFrame;
  internal CApCableEditBoxControl PassNearFrameEditBoxControl;
  internal Grid sliderOscillationGrid;
  internal TextBlock labelCableOscillation;
  internal TextBlock textBlockOscillationNone;
  internal Slider SliderOscillation;
  internal TextBlock textBlockOscillationFull;
  private bool _contentLoaded;

  public CApCableParametersNewUserControl()
  {
    this.m_isControlLoading = true;
    this.m_disposableObjects = (ICollection<IDisposable>) new List<IDisposable>();
  }

  public CApFLEXCableNewAttributeViewModel AttributeViewModel { get; private set; }

  public void InitControl(
    CApFLEXCableNewAttributeViewModel attributeViewModel)
  {
    this.InitializeComponent();
    this.AttributeViewModel = attributeViewModel ?? throw new ArgumentNullException(nameof (attributeViewModel));
    TxApplication.Options.Units.LinearUnitsChanged += new TxUnitsOptions_LinearUnitsChangedEventHandler(this.Units_LinearUnitsChanged);
    TxApplication.Options.Units.MassUnitsChanged += new TxUnitsOptions_MassUnitsChangedEventHandler(this.Units_MassUnitsChanged);
    this.AttributeViewModel.FulcrumStepSize = this.ComputeFulcrumStepSize();
    this.AttributeViewModel.MinCurvatureStepSize = CApCableUtility.ComputeRadiusStepSize();
    this.InitNumEditorControl();
    this.InitNumEditorControlWithoutUnitDecimalPlace();
    this.InitViolationEmphasizeColors();
    this.InitializePassNearEditBoxAndGetFrameValidator();
    this.SetUpDisposableObjects();
  }

  private CApFLEXFrameValidator InitializePassNearEditBoxAndGetFrameValidator()
  {
    CApFLEXFrameValidator objectValidator = new CApFLEXFrameValidator((ICApFLEXFrameSelection) new CApFLEXFrameSelection(FrameSelectionType.PassNearFrame, (ICApCableFrameStatus) this.AttributeViewModel), this.AttributeViewModel.CableScopeStatus);
    this.PassNearFrameEditBoxControl.InitializeControl((ITxValidator) objectValidator);
    return objectValidator;
  }

  private void SetUpDisposableObjects()
  {
    this.m_disposableObjects.Add((IDisposable) new CApCablePassNearFrameControlBinder((ICApCableEditBoxControl) this.PassNearFrameEditBoxControl, (ICApFLEXCableAttributeViewModel) this.AttributeViewModel));
  }

  private void InitNumEditorControl()
  {
    this.UpdateLinearUnitText();
    this.UpdateMassUnitText();
    this.textblockDensity.Text = $"{this.textblockDensity.Text} ({CApFLEXStringTable.DensityUnitString}):";
    this.textblockMaxTwist.Content = (object) $"{this.textblockMaxTwist.Content?.ToString()} ({CApFLEXStringTable.MaxTwistUnitString}):";
    this.textblockYoungsModulus.Text = $"{this.textblockYoungsModulus.Text} ({CApFLEXStringTable.YoungsModulusUnit}):";
    this.TxInfoButtonStartEndFulcrum.TooltipContent = (object) $"{CApFLEXStringTable.ToolTipStartEndFulcrum}\n{CApFLEXStringTable.ToolTipFulcrumCondition}";
  }

  private void InitNumEditorControlWithoutUnitDecimalPlace()
  {
    this.TxNumericEditBoxYoungsModulus.DecimalPlaces = 2;
    this.TxNumericEditBoxDensity.DecimalPlaces = 2;
    this.TxNumericEditBoxMaxTwist.DecimalPlaces = 2;
  }

  private void InitViolationEmphasizeColors()
  {
    this.rectTensionViolationColor.Fill = this.ComputeBrushFromTxColor(TxFlxCableEx.TensionViolationEmphasizeColor);
    this.rectTwistViolationColor.Fill = this.ComputeBrushFromTxColor(TxFlxCableEx.TwistViolationEmphasizeColor);
    this.rectContactForceExcessViolationColor.Fill = this.ComputeBrushFromTxColor(TxFlxCableEx.ContactForceExcessViolationEmphasizeColor);
    this.rectBendingViolationColor.Fill = this.ComputeBrushFromTxColor(TxFlxCableEx.BendingViolationEmphasizeColor);
  }

  public void UpdateLinearUnitText()
  {
    this.labelCableFulcrumLength.Text = CApFLEXStringTable.CableFulcrumLengthLabel + CApCableUtility.LinearUnitLabelText();
    this.textblockMinCurvatuceRadius.Content = (object) (CApFLEXStringTable.CableMinCurvatureRadiusLabel + CApCableUtility.LinearUnitLabelText());
  }

  public void UpdateMassUnitText()
  {
    this.textblockMaxTension.Text = CApFLEXStringTable.CableMaxTensionLabel + CApCableUtility.MassUnitLabelText();
    this.textblockMaxContactForce.Content = (object) (CApFLEXStringTable.CableMaxContactForce + CApCableUtility.MassUnitLabelText());
  }

  private double ComputeFulcrumStepSize()
  {
    double fulcrumStepSize;
    switch ((int) TxApplication.Options.Units.LinearUnit)
    {
      case 0:
        fulcrumStepSize = 10.0;
        break;
      case 1:
        fulcrumStepSize = 1.0;
        break;
      case 2:
      case 3:
      case 4:
        fulcrumStepSize = 0.1;
        if (TxApplication.Options.Units.LinearDigitsAfterDecimalPoint == 0)
        {
          fulcrumStepSize = 1.0;
          break;
        }
        break;
      default:
        fulcrumStepSize = 1.0;
        break;
    }
    return fulcrumStepSize;
  }

  private Brush ComputeBrushFromTxColor(TxColor txColor)
  {
    return (Brush) new SolidColorBrush(Color.FromRgb(txColor.Red, txColor.Green, txColor.Blue));
  }

  private void Units_LinearUnitsChanged(
    object sender,
    TxUnitsOptions_LinearUnitsChangedEventArgs args)
  {
    this.UpdateLinearUnitText();
  }

  private void Units_MassUnitsChanged(object sender, TxUnitsOptions_MassUnitsChangedEventArgs args)
  {
    this.UpdateMassUnitText();
  }

  public void DidDialogClose()
  {
    TxApplication.Options.Units.LinearUnitsChanged -= new TxUnitsOptions_LinearUnitsChangedEventHandler(this.Units_LinearUnitsChanged);
    TxApplication.Options.Units.MassUnitsChanged -= new TxUnitsOptions_MassUnitsChangedEventHandler(this.Units_MassUnitsChanged);
    this.ClearDisposableObjects();
  }

  private void ClearDisposableObjects()
  {
    foreach (IDisposable disposableObject in (IEnumerable<IDisposable>) this.m_disposableObjects)
      disposableObject.Dispose();
    this.m_disposableObjects.Clear();
  }

  private void TxNumericEditBoxYoungsModulus_TentativeValueChanged(
    object sender,
    TxNumericEditBoxCtrl_TentativeValueChangedEventArgs args)
  {
    if (this.m_isControlLoading)
      return;
    this.AttributeViewModel.SeYoungsModulusNoEventSent(args.TentativeValue);
  }

  private void TxNumericEditBoxDensity_TentativeValueChanged(
    object sender,
    TxNumericEditBoxCtrl_TentativeValueChangedEventArgs args)
  {
    if (this.m_isControlLoading)
      return;
    this.AttributeViewModel.SetDensityNoEventSent(args.TentativeValue);
  }

  private void TxNumericEditBoxFulcrumLength_TentativeValueChanged(
    object sender,
    TxNumericEditBoxCtrl_TentativeValueChangedEventArgs args)
  {
    if (this.m_isControlLoading)
      return;
    this.AttributeViewModel.SetFulcrumNoEventSent(args.TentativeValue * TxApplication.Options.Units.LinearMultiplier);
  }

  private void TxNumericEditBoxMaxTension_TentativeValueChanged(
    object sender,
    TxNumericEditBoxCtrl_TentativeValueChangedEventArgs args)
  {
    if (this.m_isControlLoading)
      return;
    double num;
    try
    {
      num = (double) new CApNewtonToKillogramConverter().ConvertBack((object) (CApCableUtility.ConvertStringToDouble(this.TxNumericEditBoxMaxTension.Text) * TxApplication.Options.Units.MassMultiplier), this.TxNumericEditBoxMaxTension.GetType(), (object) this.TxNumericEditBoxMaxTension, CultureInfo.CurrentCulture);
    }
    catch (FormatException ex)
    {
      num = 0.0;
    }
    this.AttributeViewModel.SetMaxTentionNoEventSent(num);
  }

  private void TxNumericEditBoxMaxTwist_TentativeValueChanged(
    object sender,
    TxNumericEditBoxCtrl_TentativeValueChangedEventArgs args)
  {
    if (this.m_isControlLoading)
      return;
    this.AttributeViewModel.SetMaxTwistAngleNoEventSent(args.TentativeValue * TxApplication.Options.Units.AngularMultiplier);
  }

  private void TxNumericEditBoxMaxContactForce_TentativeValueChanged(
    object sender,
    TxNumericEditBoxCtrl_TentativeValueChangedEventArgs args)
  {
    if (this.m_isControlLoading)
      return;
    double num;
    try
    {
      num = (double) new CApNewtonToKillogramConverter().ConvertBack((object) (CApCableUtility.ConvertStringToDouble(this.TxNumericEditBoxMaxContactForce.Text) * TxApplication.Options.Units.MassMultiplier), this.TxNumericEditBoxMaxContactForce.GetType(), (object) this.TxNumericEditBoxMaxContactForce, CultureInfo.CurrentCulture);
    }
    catch (FormatException ex)
    {
      num = 0.0;
    }
    this.AttributeViewModel.SetMaxContactForceNoEventSent(num);
  }

  private void TxNumericEditBoxMinCurvatuceRadius_TentativeValueChanged(
    object sender,
    TxNumericEditBoxCtrl_TentativeValueChangedEventArgs args)
  {
    if (this.m_isControlLoading)
      return;
    this.AttributeViewModel.SetMinCurvatureRadiusNoEventSent(args.TentativeValue * TxApplication.Options.Units.LinearMultiplier);
  }

  public void StartOfLoading() => this.m_isControlLoading = true;

  public void EndOfLoading() => this.m_isControlLoading = false;

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/flexibles/usercontrols/capcableparametersnewusercontrol.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  internal Delegate _CreateDelegate(Type delegateType, string handler)
  {
    return Delegate.CreateDelegate(delegateType, (object) this, handler);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.parameterStackPanel = (StackPanel) target;
        break;
      case 2:
        this.parameterGrid = (Grid) target;
        break;
      case 3:
        this.densityRow = (RowDefinition) target;
        break;
      case 4:
        this.fulcrumRow = (RowDefinition) target;
        break;
      case 5:
        this.maxTensionRow = (RowDefinition) target;
        break;
      case 6:
        this.maxTwistRow = (RowDefinition) target;
        break;
      case 7:
        this.minCurvatureRadiusRow = (RowDefinition) target;
        break;
      case 8:
        this.passNearRow = (RowDefinition) target;
        break;
      case 9:
        this.oscillationSliderRow = (RowDefinition) target;
        break;
      case 10:
        this.textblockDensity = (TextBlock) target;
        break;
      case 11:
        this.TxNumericEditBoxDensity = (TxNumericEditBoxControl) target;
        this.TxNumericEditBoxDensity.TentativeValueChanged += new TxNumericEditBoxCtrl_TentativeValueChangedEventHandler(this.TxNumericEditBoxDensity_TentativeValueChanged);
        break;
      case 12:
        this.textblockYoungsModulus = (TextBlock) target;
        break;
      case 13:
        this.TxNumericEditBoxYoungsModulus = (TxNumericEditBoxControl) target;
        this.TxNumericEditBoxYoungsModulus.TentativeValueChanged += new TxNumericEditBoxCtrl_TentativeValueChangedEventHandler(this.TxNumericEditBoxYoungsModulus_TentativeValueChanged);
        break;
      case 14:
        this.labelCableFulcrumLength = (TextBlock) target;
        break;
      case 15:
        this.TxNumericEditBoxFulcrumLength = (TxNumericEditBoxControl) target;
        this.TxNumericEditBoxFulcrumLength.TentativeValueChanged += new TxNumericEditBoxCtrl_TentativeValueChangedEventHandler(this.TxNumericEditBoxFulcrumLength_TentativeValueChanged);
        break;
      case 16 /*0x10*/:
        this.TxInfoButtonStartEndFulcrum = (TxInfoButton) target;
        break;
      case 17:
        this.tensionGrid = (Grid) target;
        break;
      case 18:
        this.tensionColumn = (ColumnDefinition) target;
        break;
      case 19:
        this.rectTensionViolationColor = (Rectangle) target;
        break;
      case 20:
        this.textblockMaxTension = (TextBlock) target;
        break;
      case 21:
        this.TxNumericEditBoxMaxTension = (TxNumericEditBoxControl) target;
        this.TxNumericEditBoxMaxTension.TentativeValueChanged += new TxNumericEditBoxCtrl_TentativeValueChangedEventHandler(this.TxNumericEditBoxMaxTension_TentativeValueChanged);
        break;
      case 22:
        this.twistGrid = (Grid) target;
        break;
      case 23:
        this.twistColumn = (ColumnDefinition) target;
        break;
      case 24:
        this.rectTwistViolationColor = (Rectangle) target;
        break;
      case 25:
        this.textblockMaxTwist = (CheckBox) target;
        break;
      case 26:
        this.TxNumericEditBoxMaxTwist = (TxNumericEditBoxControl) target;
        this.TxNumericEditBoxMaxTwist.TentativeValueChanged += new TxNumericEditBoxCtrl_TentativeValueChangedEventHandler(this.TxNumericEditBoxMaxTwist_TentativeValueChanged);
        break;
      case 27:
        this.contactForceGrid = (Grid) target;
        break;
      case 28:
        this.contactForceColumn = (ColumnDefinition) target;
        break;
      case 29:
        this.rectContactForceExcessViolationColor = (Rectangle) target;
        break;
      case 30:
        this.textblockMaxContactForce = (CheckBox) target;
        break;
      case 31 /*0x1F*/:
        this.TxNumericEditBoxMaxContactForce = (TxNumericEditBoxControl) target;
        this.TxNumericEditBoxMaxContactForce.TentativeValueChanged += new TxNumericEditBoxCtrl_TentativeValueChangedEventHandler(this.TxNumericEditBoxMaxContactForce_TentativeValueChanged);
        break;
      case 32 /*0x20*/:
        this.curvatureRadiusGrid = (Grid) target;
        break;
      case 33:
        this.curvatureColumn = (ColumnDefinition) target;
        break;
      case 34:
        this.rectBendingViolationColor = (Rectangle) target;
        break;
      case 35:
        this.textblockMinCurvatuceRadius = (CheckBox) target;
        break;
      case 36:
        this.TxNumericEditBoxMinCurvatuceRadius = (TxNumericEditBoxControl) target;
        this.TxNumericEditBoxMinCurvatuceRadius.TentativeValueChanged += new TxNumericEditBoxCtrl_TentativeValueChangedEventHandler(this.TxNumericEditBoxMinCurvatuceRadius_TentativeValueChanged);
        break;
      case 37:
        this.labelCablePassNearFrame = (TextBlock) target;
        break;
      case 38:
        this.PassNearFrameEditBoxControl = (CApCableEditBoxControl) target;
        break;
      case 39:
        this.sliderOscillationGrid = (Grid) target;
        break;
      case 40:
        this.labelCableOscillation = (TextBlock) target;
        break;
      case 41:
        this.textBlockOscillationNone = (TextBlock) target;
        break;
      case 42:
        this.SliderOscillation = (Slider) target;
        break;
      case 43:
        this.textBlockOscillationFull = (TextBlock) target;
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
